package com.lagou.edu.service;

import com.alibaba.fastjson.JSON;
import com.lagou.edu.bean.Position;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 陶肖寅
 * @version V1.0
 * <p>
 * date: 2020/10/8 11:08 上午
 * email:fromlx@126.com
 * @Copyright: 2020-2021 城云科技(中国)有限公司
 * </p>
 */
@Service
public class PositionService  implements IPositionService{
    @Autowired
    SolrClient solrClient;
    @Override
    public  List<Position> query(String positionName) throws IOException, SolrServerException {
        //创建查询对象
        SolrQuery query = new SolrQuery();
        // 设置查询条件
        query.set("q", "positionName:" + positionName);
        query.set("start", "0");
        query.set("rows", "100");
        // 执行查询
        QueryResponse queryResponse = solrClient.query(query);
        // 获取文档列表
        SolrDocumentList documentList = queryResponse.getResults();
        if(documentList.size() < 5){
            //如果条件不够5条，则增加条件
            query.set("q", "positionName:" + positionName + " AND positionAdvantage:美女多、员工福利好");
            queryResponse = solrClient.query(query);
            documentList = queryResponse.getResults();
        }
        List<Position> list = new ArrayList<>();
        for (SolrDocument solrDocument : documentList) {
            Position position = JSON.parseObject(JSON.toJSONBytes(solrDocument), Position.class);
            list.add(position);
            System.out.println("id:" + solrDocument.get("id"));
            System.out.println("positionName:" + solrDocument.get("positionName"));
            System.out.println("companyName:" + solrDocument.get("companyName"));
            System.out.println("positionAdvantage:" + solrDocument.get("positionAdvantage"));
//            System.out.println("desc:" + solrDocument.get("desc"));
//            System.out.println("price:" + solrDocument.get("price"));
        }
        return list;
    }
}
